/**
 * 
 */
package com.cmpanyandi.elearning.utils.db;

import java.util.ArrayList;
import java.util.List;

import com.cmpanyandi.elearning.utils.StringUtils;
import com.cmpanyandi.elearning.utils.db.query.EqualCondition;
import com.cmpanyandi.elearning.utils.db.query.LikeCondition;
import com.cmpanyandi.elearning.utils.db.query.QueryCondition;
import com.cmpanyandi.elearning.utils.db.query.LikeCondition.LikeType;

/**
 * This class defines query to be executed.
 * 
 * @author wojtek
 * 
 */
public class QueryBuilder {

    private String selectClause;
    private String fromClause;
    private String orderByClause = "";

    private List<QueryCondition> contentValues = new ArrayList<QueryCondition>();

    /**
     * This method add equal condition for binding
     * 
     * @param propertyName
     *            - name of property
     * @param value
     *            - property value.
     */
    public QueryBuilder addWhereEqual(String propertyName, String value) {
	contentValues.add(new EqualCondition(propertyName, value));
	return this;
    }

    /**
     * This method add like condition.
     * 
     * @param propertyName
     *            - property name
     * @param value
     *            - property value.
     */
    public QueryBuilder addWhereLike(String propertyName, String value, LikeType likeType) {
	contentValues.add(new LikeCondition(propertyName, value, likeType));
	return this;
    }

    /**
     * @return the selectClause
     */
    public String getSelectClause() {
	return selectClause;
    }

    /**
     * Set select clause. SELECT keyword is required.
     * 
     * @param selectClause
     *            the selectClause to set
     */
    public QueryBuilder setSelectClause(String selectClause) {
	this.selectClause = selectClause;
	return this;
    }

    public QueryBuilder setSelectClause(String[] columns) {
	String select = "select ";
	
	for (String column : columns) {
	    select += column + ", ";
	}
		
	this.selectClause = StringUtils.substringBeforeLast(select, ",");
	return this;
    }
    
    /**
     * @return the fromClause
     */
    public String getFromClause() {
	return fromClause;
    }

    /**
     * Set from clause. FROM keyword is required.
     * 
     * @param fromClause
     *            the fromClause to set
     * @return - instance of this object.
     */
    public QueryBuilder setFromClause(String fromClause) {
	
	if (!fromClause.toLowerCase().startsWith("from")) {
	    
	}
	this.fromClause = fromClause.toLowerCase().startsWith("from") ? fromClause : " from " + fromClause;
	return this;
    }

    /**
     * @return the orderByClause
     */
    public String getOrderByClause() {
	return orderByClause;
    }

    /**
     * Set order by clause. Order by keyword is required.
     * 
     * @param orderByClause
     *            - the orderByClause to set
     * @return - instance of this object.
     */
    public QueryBuilder setOrderByClause(String orderByClause) {
	this.orderByClause = orderByClause;
	return this;
    }

    /**
     * @return the contentValues
     */
    public List<QueryCondition> getContentValues() {
	return contentValues;
    }

    /**
     * @param contentValues
     *            the contentValues to set
     * @return - instance of this object.
     */
    public QueryBuilder setContentValues(List<QueryCondition> contentValues) {
	this.contentValues = contentValues;
	return this;
    }

    private String getWhereClause() {
	String where = "";

	if (contentValues.size() > 0) {
	    where = " where ";
	    for (QueryCondition condition : contentValues) {
		where += " " + condition.getConditionText() + " and";
	    }

	    where = where.substring(0, where.length() - 3);
	}

	return where;

    }

    /**
     * This method return prepare statement sql text. This query text contains ?
     * marks characters.
     * 
     * @return - query string.
     */
    public String getQueryString() {
	return getSelectClause() + getFromClause() + getWhereClause() + getOrderByClause();
    }

    /**
     * This method return bindings values to replace questions marks characters.
     * 
     * @return - String[] array of bindings properties values.
     */
    public String[] getBindingsValues() {

	String retVal = "";

	for (QueryCondition condition : contentValues) {
	    retVal += condition.getValue() + ",";
	}

	if (retVal.contains(",")) {
	    retVal = retVal.substring(0, retVal.length() - 1);
	}

	return retVal.split(",");

    }
}
